package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.entity.Repair;
import com.fz.entity.TNotice;
import com.fz.mapper.TNoticeMapper;
import com.fz.service.TNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fz
 * @since 2024-08-22
 */
@Service
public class TNoticeServiceImpl extends ServiceImpl<TNoticeMapper, TNotice> implements TNoticeService {
    @Resource
    TNoticeMapper tNoticeMapper;

    @Override
    public Page<TNotice> getList(Integer pageNo, Integer pageSize) {
        QueryWrapper<TNotice> wrapper = new QueryWrapper<>();
        Page<TNotice>tNoticePage;
        wrapper.orderByDesc("time");
        tNoticePage = this.page(new Page<>(pageNo, pageSize), wrapper);
        return tNoticePage;
    }
    @Override
    public Page<TNotice> search(String input, Integer pageNo, Integer pageSize){
        QueryWrapper<TNotice> wrapper=new QueryWrapper<>();
        Page<TNotice>tNoticePage;
        wrapper.like("title",input)
                .orderByDesc("time");
        tNoticePage = this.page(new Page<>(pageNo, pageSize), wrapper);
        return this.page(new Page<>(pageNo, pageSize),wrapper);
    }
    @Override
    public Page<TNotice> showList(Integer pageNo, Integer pageSize) {
        QueryWrapper<TNotice> wrapper = new QueryWrapper<>();
        Page<TNotice>tNoticePage;
        wrapper.orderByDesc("time");
        tNoticePage = this.page(new Page<>(pageNo, pageSize), wrapper);
        return this.page(new Page<>(pageNo, pageSize),wrapper);
    }
    @Override
    public Boolean add(String title, String content, LocalDateTime time){
    return this.save(new TNotice(
            title,content,time
    ));}
    @Override
    public boolean update(Long id,String title,String content){
    return this.updateById(new TNotice(
            id,title,content
    ));
    }
    public boolean update(Long id,Long view){
        return this.updateById(new TNotice(
                id,view
        ));
    }
    @Override
    public boolean del(Long id,Integer statue,Integer statu){
        return this.updateById(new TNotice(id,statue,statu));
    }
    @Override
    public List<TNotice> search(String input){
        QueryWrapper<TNotice> wrapper = new QueryWrapper<>();
        wrapper.like("title", input);
        return this.list(wrapper);

    }
    @Override
    public String detail(Long id){return this.getById(id).getContent();}
    @Override
    public Long getByTNoticeId(Long tNoticeId){//根据id计算浏览量
        return tNoticeMapper.getByTNoticeId(tNoticeId);
    }
    @Override
    public List<TNotice> updateview(List<TNotice> tNoticeList) {
        List<TNotice> updatedTNoticeList = new ArrayList<>();

        for (TNotice notice : tNoticeList) {//循环表格里的公告
            Long tNoticeId = notice.getId();//获取id
            Long View = this.getByTNoticeId(tNoticeId);//计算浏览量

            // view值，我们需要更新这个值
            //UpdateWrapper<TNotice> updateWrapper = new UpdateWrapper<>();
            //updateWrapper.eq("id", tNoticeId);
            //updateWrapper.set("view", View);

            // 如果更新成功，将通知添加到更新列表中
            if (this.update(tNoticeId,View)) {//如果更新浏览量成功
                updatedTNoticeList.add(notice);//把更新后的公告放到新的表里
                System.out.println("更新浏览量成功，通知ID: " + tNoticeId);
            } else {
                System.out.println("更新浏览量错误，通知ID: " + tNoticeId);
            }
        }

        return updatedTNoticeList;
    }

}
